package com.shopping.dev.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shopping.dev.common.enums.CommentLevelEnum;
import com.shopping.dev.common.enums.YesOrNoEnum;
import com.shopping.dev.mapper.*;
import com.shopping.dev.pojo.*;
import com.shopping.dev.service.ItemsService;
import com.shopping.dev.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class ItemsServiceImpl implements ItemsService {
    @Autowired
    private ItemsMapper itemsMapper;
    @Autowired
    private ItemsParamMapper itemsParamMapper;
    @Autowired
    private ItemsImgMapper itemsImgMapper;
    @Autowired
    private ItemsCommentsMapper itemsCommentsMapper;
    @Autowired
    private ItemsSpecMapper itemsSpecMapper;
    @Autowired
    private ItemsMapperCustom itemsMapperCustom;

    @Override
    public Items queryItemById(String itemId) {
        Items items = itemsMapper.selectByPrimaryKey(itemId);
        return items;
    }

    @Override
    public List<ItemsImg> queryItemImgById(String itemId) {
        Example example = new Example(ItemsImg.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId", itemId);
        List<ItemsImg> itemsImgs = itemsImgMapper.selectByExample(example);
        return itemsImgs;
    }

    @Override
    public ItemsSpec queryItemsSpecById(String itemId) {

        ItemsSpec itemsImgs = itemsSpecMapper.selectByPrimaryKey(itemId);
        return itemsImgs;
    }

    @Override
    public List<ItemsParam> queryItemsParamById(String itemId) {
        Example example = new Example(ItemsSpec.class);
        example.createCriteria().andEqualTo("itemId", itemId);
        List<ItemsParam> itemsParams = itemsParamMapper.selectByExample(example);
        return itemsParams;
    }

    @Override
    public CommentLevelCountsVO queryCommentLevel(String itemId, Integer level) {
        CommentLevelCountsVO commentLevelCountsVO = new CommentLevelCountsVO();
        Integer goodCount = queryCommentCounts(itemId, CommentLevelEnum.GOOD.getCode());
        Integer normalCount = queryCommentCounts(itemId, CommentLevelEnum.NORMAL.getCode());
        Integer badCount = queryCommentCounts(itemId, CommentLevelEnum.BAD.getCode());
        commentLevelCountsVO.setBadCounts(badCount);
        commentLevelCountsVO.setNormalCounts(normalCount);
        commentLevelCountsVO.setGoogCounts(goodCount);
        commentLevelCountsVO.setTotalCounts(goodCount+normalCount+badCount);
        return commentLevelCountsVO;
    }
    private Integer queryCommentCounts(String itemId, Integer level) {
        ItemsComments itemsComments = new ItemsComments();
        itemsComments.setItemId(itemId);
        if (level != null) {
            itemsComments.setCommentLevel(level);
        }
        Integer count = itemsCommentsMapper.selectCount(itemsComments);
        return count;
    }
    @Override
    public PagedGridResult queryComments(String itemId, Integer level, Integer page, Integer pageSize) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("itemId",itemId);
        paramMap.put("commentLevel",level);
        PageHelper.startPage(page,pageSize);
        List<ItemCommentVO> list = itemsMapperCustom.queryComments(paramMap);
        return setter(list,page);
    }
    private PagedGridResult setter(List<?> list , Integer page){
        PageInfo<?> pageInfo = new PageInfo(list);
        PagedGridResult result = new PagedGridResult();
        result.setPage(page);
        result.setRows(list);
        result.setTotal(pageInfo.getPages());
        result.setRecords(pageInfo.getTotal());
        return result;
    }

    @Override
    public PagedGridResult searchItems(String keywords, String sort, Integer page, Integer pageSize) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("keywords",keywords);
        paramMap.put("sort",sort);
        PageHelper.startPage(page,pageSize);
        List<SearchItemsVO> searchItemsVOS = itemsMapperCustom.searchItems(paramMap);
        return setter(searchItemsVOS,page);
    }

    @Override
    public PagedGridResult searchItemsThirdByCatId(Integer catId, String sort, Integer page, Integer pageSize) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("catId",catId);
        paramMap.put("sort",sort);
        PageHelper.startPage(page,pageSize);
        List<SearchItemsVO> searchItemsVOS = itemsMapperCustom.searchItemsThirdByCatId(paramMap);
        return setter(searchItemsVOS,page);
    }

    @Override
    public List<ShopCartVO> queryItemsBySpecIds(String itemSpecIds) {
        String[] ids = itemSpecIds.split(",");
        List<ShopCartVO> list = itemsMapperCustom.queryItemsBySpecIds(Arrays.asList(ids));
        return list;
    }

    @Override
    public List<ItemsSpec> queryItemsSpecList(String itemId) {
        Example example = new Example(ItemsSpec.class);
        example.createCriteria().andEqualTo("itemId", itemId);
        List<ItemsSpec> itemsImgs = itemsSpecMapper.selectByExample(example);
        return itemsImgs;
    }

    @Override
    public String queryItemMainImgById(String itemId) {
        ItemsImg img = new ItemsImg();
        img.setItemId(itemId);
        img.setIsMain(YesOrNoEnum.YES.getCode());
        ItemsImg result = itemsImgMapper.selectOne(img);
        return result==null?"":result.getUrl();
    }

    @Override
    public void decreaseItemSpecStock(String specid, Integer buyCounts) {
        int i = itemsMapperCustom.decreaseItemSpecStock(specid, buyCounts);
        if (i<1){
            throw new RuntimeException("库存不足");
        }
    }


}
